<template>
  <div class="register-container">
    <el-card class="register-card">
      <h1 class="register-title">用户注册</h1>
      <div class="register-form">
        <el-form :model="registerForm" ref="registerFormRef" :rules="rules" hide-required-asterisk>
          <el-row>
            <el-col :span="24">
              <el-form-item label="用户名" label-width="60px" prop="username">
                <el-input v-model="registerForm.username" autocomplete="off" maxlength="10" show-word-limit />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="密码" label-width="60px" prop="password">
                <el-input type="password" v-model="registerForm.password" autocomplete="off" show-password />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="确认密码" label-width="70px" prop="confirmPassword">
                <el-input
                  type="password"
                  v-model="registerForm.confirmPassword"
                  autocomplete="off"
                  show-password
                  style="width: 100%;"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item>
            <el-button type="primary" @click="handleRegister(registerFormRef)" style="width: 100%;">注册</el-button>
          </el-form-item>
        </el-form>
        <el-row style="text-align: right;">
          <el-col>
            <el-text class="w-150px mb-2">
              已有帐号？<RouterLink to="/login" style="font-size: 1.2em;">去登录</RouterLink>
            </el-text>
          </el-col>
        </el-row>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter, RouterLink } from 'vue-router'
import { ElMessage } from 'element-plus'
import axios from 'axios'

const registerForm = ref({
  username: '',
  password: '',
  confirmPassword: ''
})
const registerFormRef = ref()
const rules = reactive({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码至少6位', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请确认密码', trigger: 'blur' },
    {
      validator: (rule, value) => value === registerForm.value.password,
      message: '两次输入的密码不一致',
      trigger: 'blur'
    }
  ]
})

const router = useRouter()
async function handleRegister(formEl) {
  if (!formEl) return
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      try {
        const res = await axios.post('http://localhost:8080/api/register', {
          username: registerForm.value.username,
          password: registerForm.value.password
        })
        if (res.data.code === 200) {
          ElMessage.success('注册成功，请登录')
          router.push('/login')
        } else {
          ElMessage.error(res.data.msg || '注册失败')
        }
      } catch (e) {
        ElMessage.error('注册失败，请检查网络或联系管理员')
      }
    } else {
      console.log('error submit!', fields)
    }
  })
}
</script>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-image: url('@/assets/login1.jpg');
  background-size: cover;
  background-position: center;
}

.register-card {
  width: 420px;
  padding: 20px;
  background-color: #fefefe;
}

.register-title {
  text-align: center;
  margin-bottom: 20px;
  color: #409eff;
}

.register-form {
  width: 100%;
}
</style>